/**
 * 倒计时方法， 参数是想到哪一天的时间格式
 * @param target
 * @returns {string}
 */
function countDown(target) {
    // 利用+new Date的方式转成时间戳的形式
    const targetTime = +new Date(target)
    const nowTime = + new Date()

    // 将时间戳的毫秒数转成秒数
    const time = (targetTime - nowTime) / 1000
    const d = time / 60 / 60 / 24
    const h = time / 60 / 60 % 24
    const m = time / 60 % 60
    const s = time % 60

    return `${parseInt(d)}天 ${parseInt(h)}小时 ${parseInt(m)}分钟 ${parseInt(s)}秒`
}

// console.log(countDown("2022-12-30 12:00:00"));

function countDiff(target) {
  const nowDate = +new Date();
  const targetDate = +new Date(target);
  const diff = (targetDate - nowDate) / 1000;
  const s = parseInt(diff % 60);
  const m = parseInt((diff / 60) % 60);
  const h = parseInt((diff / 60 / 60) % 24);
  const d = parseInt(diff / 60 / 60 / 24);
  const H = parseInt(diff / 60 / 60 / 24 / 30);
  const Y = parseInt(diff / 60 / 60 / 24 / 30 / 12);

  return `${Y}年${H}月${d}天${h}小时${m}分钟${s}秒`;
}
console.log(countDiff("2022-12-30 23:59:59"));